package com.tusuo.stds.ui.question;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.tusuo.stds.Constants;
import com.tusuo.stds.rest.LevelDetail;
import com.tusuo.stds.rest.Question;
import com.tusuo.stds.ui.questionbank.QuestionBankTempActivity_;
import com.tusuo.stds.ui.similarity.SimilarityTempActivity_;
import com.tusuo.stds.util.webview.WebViewManager;

import java.util.List;

/**
 * Created by developer on 17-6-12.
 */

public class QuestionAdapterForSimilarity extends PagerAdapter {

    private LevelDetail mDetail;
    private List<View> mListView;
    private String mKey;
    private Activity mActivity;
    private List<WebViewManager> mWebViewManagerList;
    private String mLoadUrl;
    private int mFinishCount;


   public QuestionAdapterForSimilarity(List<View> listView, LevelDetail detail, List<WebViewManager> webViewManagerList, String key, Activity activity, String loadUrl, int finishCount) {
        mDetail = detail;
        mListView = listView;
        mWebViewManagerList = webViewManagerList;
        mKey = key;
        mActivity = activity;
        mLoadUrl = loadUrl;
        mFinishCount = finishCount;
    }


    public void setAnswerContentReviewModeRepeat(int index) {
        WebViewManager webViewManager = mWebViewManagerList.get(index);
        webViewManager.setReviewMode();
    }


    @Override
    public int getItemPosition(Object object) {

        return POSITION_NONE;
    }


    @Override
    public int getCount() {
        return mDetail.QuestionCount;   // 首添加一个空白页
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {  // 首多了1个空白页

        View view = mListView.get(position);

        if (view.getParent() == null) {
            Question question = mDetail.QuestionList.get(position);

            WebViewManager webViewManager = mWebViewManagerList.get(position);
            webViewManager.init();
            webViewManager.loadUrl(mLoadUrl);
            if (mActivity instanceof QuestionActivity_) {
                webViewManager.registerHandlerSubmitAnswer(((QuestionActivity_)mActivity).mSubmitAnswerHandler);
            }else if (mActivity instanceof SimilarityTempActivity_) {
                webViewManager.registerHandlerSubmitAnswer(((SimilarityTempActivity_)mActivity).mSubmitAnswerHandler);
            }else if (mActivity instanceof QuestionBankTempActivity_) {
                webViewManager.registerHandlerSubmitAnswer(((QuestionBankTempActivity_)mActivity).mSubmitAnswerHandler);
            }
            webViewManager.initQuestion(question);
            if (position < mFinishCount) {
                webViewManager.setReviewMode();
            }

            container.addView(view);
        }

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }




}
